<?php

require_once 'phpmailer/class.phpmailer.php';

class Mailer
{

    protected $_receipients = array();

    protected $_replyTo = array();

    protected $_message = null;

    protected $_subject = null;

    protected $_fromEmail = null;

    protected $_fromName = null;

    static function inst()
    {
        return new self();
    }

    function addReceipient($address, $name)
    {
        $this -> _receipients[] = array(
            $address,
            $name
        );
        return $this;
    }

    function addReplyTo($address, $name)
    {
        $this -> _replyTo[] = array(
            $address,
            $name
        );
        return $this;
    }

    function setMessage($message, $data = null)
    {
        if (is_array($data))
        {
            $message = View::render($data, $message);
        }
        $this -> _message = $message;

        return $this;
    }

    function setSubject($subject, $data = null)
    {
        $this -> _subject = $subject;
        return $this;
    }

    function setFrom($email, $name)
    {
        $this -> _fromEamil = $email;
        $this -> _fromName = $name;
        return $this;
    }

    function send()
    {
        try
        {

            $mailer = new PHPMailer(0);
            $mailer ->CharSet = 'UTF8';
            $mailer -> IsSMTP();
            $mailer -> Host = 'smtp.gmail.com';
            $mailer -> Port = 587;
            $mailer -> SMTPAuth = true;
            $mailer -> SMTPSecure = 'tls';

            $mailer -> Username = 'nam.ngvan@gmail.com';
            $mailer -> Password = 'Carollyn123@';

            if (empty($this -> _fromEmail))
            {
                $this -> _fromEmail = 'nam.ngvan@gmail.com';
                $this -> _fromName = 'support@k1.vn';
            }

            $mailer -> SetFrom($this -> _fromEmail, $this -> _fromName);

            if (!empty($this -> _receipients))
            {
                foreach ($this->_receipients as $receipient)
                {
                    list($email, $name) = $receipient;
                    $mailer -> AddAddress($email, $name);
                }
            }

            if (!empty($this -> _replyTo))
            {
                foreach ($this->_replyTo as $reply)
                {
                    list($email, $name) = $reply;
                    $mailer -> AddReplyTo($email, $name);
                }
            }

            $mailer -> Subject = $this -> _subject;

            $mailer -> MsgHTML($this -> _message);

            $mailer -> Send();
        }
        catch(Exception $e)
        {
            $data = array(
                'subject' => $this -> _subject,
                'message' => $this -> _message,
                'from_email' => $this -> _fromEmail,
                'from_name' => $this -> _fromName,
                'recepient' => $this -> _receipients,
                'error' => $e -> getMessage(),
                'is_sended' => 0,
                'timestamp' => time()
            );
            
            Mongo_Database::inst() -> selectCollection('mails') -> insert($data);
        }
    }

}
